package com.leyou.service;

import com.leyou.domian.Category;
import com.leyou.exception.LyException;
import com.leyou.exception.domain.ExceptionEnum;
import com.leyou.mapper.CategoryMapper;
import com.leyou.dto.CategoryDto;
import com.leyou.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Transactional
@Service
public class CategoryService {

    @Autowired(required = false)
    CategoryMapper categoryMapper;

    public List<CategoryDto> findByParent(Long pid) {

        Category category = new Category();
        category.setParentId(pid);
        List<Category> list = categoryMapper.select(category);

        if (list == null || list.isEmpty()) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, CategoryDto.class);
    }

    public List<CategoryDto> findCategoryByIds(List<Long> ids) {
        List<Category> categoryList = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(categoryList)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList, CategoryDto.class);
    }

    public List<CategoryDto> findCategoriesByIds(List<Long> ids) {
        List<Category> list = categoryMapper.selectByIdList(ids);
        List<CategoryDto> categoryDtos = BeanHelper.copyWithCollection(list, CategoryDto.class);
        if (CollectionUtils.isEmpty(categoryDtos)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categoryDtos;
    }
}
